/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.Common;
using EXtremecode.PresentationLayer;

public partial class ASCXButton : System.Web.UI.UserControl, IButtonControl, IASCXButton
{
    private string navigateUrl = string.Empty;
    private string onClientClick = string.Empty;
    private bool autoPostBack = true;
    public event EventHandler Click;
    private string target = string.Empty;
    string imageURL = string.Empty;

    private bool isDefault = false;
    public bool IsDefault
    {
        get
        {
            return isDefault;
        }
        set
        {
            isDefault = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        btn.Click +=new EventHandler(btn_Click);

        //get userProfile
        IUserProfile profile = SessionRepository.Instance.UserProfile;
        if (profile != null)
        {
            profile.Decorator.DecorateButton(tbl, Text,imageURL);
        }
        else
        {
            //default
            new XcodeDecorator().DecorateButton(tbl, Text,imageURL);

        }

      
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        string documnetLocation = string.Empty;
        if (!string.IsNullOrEmpty(navigateUrl) || !string.IsNullOrEmpty(onClientClick))
        {
            if (!string.IsNullOrEmpty(navigateUrl))
            {
                if (target.Equals("popup"))
                {
                    documnetLocation = string.Format("window.open('{0}')", navigateUrl);
                }
                else
                {
                    documnetLocation = string.Format("document.location='{0}'", navigateUrl);
                }
            }
            btn.Attributes.Add("onclick", string.Format("try{{{0};{1};return false;}}  catch(err){{return false;}}",
                documnetLocation, onClientClick));
        }

        tbl.Attributes.Add("onclick",
            string.Format("document.getElementById('{0}').click();", btn.ClientID));

        tbl.ToolTip = btn.ToolTip;

        if (IsDefault)
        {
            Page.Form.DefaultButton = btn.UniqueID;
        }
       
    }
    void btn_Click(object sender, ImageClickEventArgs e)
    {
        if (Click != null) Click(sender, e);
    }
    void btn_Click(object sender, EventArgs e)
    {
        if (Click != null) Click(sender, e);
    }
    public string Text
    {
        get
        {
            return btn.Text;
        }
        set
        {
             btn.Text = value;
        }
    }

    public string ImageURL
    {
        get
        {
            return imageURL;
        }
        set
        {
            imageURL = value;
        }
    }

    public string NavigateUrl
    {
        get
        {
            return navigateUrl;
        }
        set
        {
            navigateUrl = value;
        }
    }

    public string ToolTip
    {
        get
        {
            return btn.ToolTip;
        }
        set
        {
            btn.ToolTip = value;
        }
    }

    public string OnClientClick
    {
        get
        {
            return onClientClick;
        }
        set
        {
            onClientClick = value;
        }
    }
    public bool CausesValidation
    {
        get
        {
            return btn.CausesValidation;

        }
        set
        {
            btn.CausesValidation = value;
        }
    }
    public string CommandName
    {
        get
        {
            return btn.CommandName;
        }
        set
        {
            btn.CommandName = value;
        }
    }
    public string Target
    {
        get
        {
            return target;
        }
        set
        {
            target = value;
        }
    }

    public bool AutoPostBack
    {
        get
        {
            return btn.CausesValidation;
        }
        set
        {
            btn.CausesValidation = value;
        }
    }

    #region IButtonControl Members


    public event CommandEventHandler Command;

    public string CommandArgument
    {
        get
        {
            return string.Empty;
        }
        set
        {
        }
    }

    public string PostBackUrl
    {
        get
        {
            return string.Empty;
        }
        set
        {
        }
    }

    public string ValidationGroup
    {
        get
        {
            return btn.ValidationGroup;
        }
        set
        {
            btn.ValidationGroup = value;
        }
    }


    #endregion

    public new AttributeCollection Attributes
    {
        get
        {
            return btn.Attributes;
        }

    }

}
